package com.boluo.businessManagement.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.boluo.businessManagement.domain.BusMentGoodsStore;
import com.boluo.businessManagement.domain.BusMentStoreListinfo;
import com.boluo.businessManagement.dto.BusMentGoodsDTO;
import com.boluo.businessManagement.mapper.BusMentGoodsStoreMapper;
import com.boluo.businessManagement.mapper.BusMentStoreListinfoMapper;
import com.boluo.businessManagement.vo.BusMentGoodVO;
import com.boluo.common.constant.BusinessManagementConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boluo.businessManagement.mapper.BusMentGoodsMapper;
import com.boluo.businessManagement.domain.BusMentGoods;
import com.boluo.businessManagement.service.IBusMentGoodsService;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 物品信息Service业务层处理
 * 
 * @author boluo
 * @date 2023-05-02
 */
@Service
public class BusMentGoodsServiceImpl implements IBusMentGoodsService 
{
    @Resource
    private BusMentGoodsMapper busMentGoodsMapper;
    @Resource
    private BusMentGoodsStoreMapper busMentGoodsStoreMapper;
    /**
     * 查询物品信息
     * 
     * @param id 物品信息主键
     * @return 物品信息
     */
    @Override
    public BusMentGoods selectBusMentGoodsById(Long id)
    {
        return busMentGoodsMapper.selectBusMentGoodsById(id);
    }

    /**
     * 查询物品信息列表
     * 
     * @param busMentGoodsDTO 物品信息
     * @return 物品信息
     */
    @Override
    public List<BusMentGoodVO> selectBusMentGoodsList(BusMentGoodsDTO busMentGoodsDTO)
    {
        //
        List<BusMentGoodVO> busMentGoodVOS = new ArrayList<>();
        List<BusMentGoods> busMentGoods = busMentGoodsMapper.selectBusMentGoodsList(busMentGoodsDTO);
        for (BusMentGoods busMentGood : busMentGoods) {
            Long goodId = busMentGood.getId();
            BusMentGoodsStore goodsStore = busMentGoodsStoreMapper.selectByGoodsId(goodId);
            BusMentGoodVO busMentGoodVO = new BusMentGoodVO();
            BeanUtils.copyProperties(busMentGood,busMentGoodVO);
            busMentGoodVO.setGoodsStore(goodsStore);
            busMentGoodVOS.add(busMentGoodVO);
        }

        return busMentGoodVOS;
    }

    /**
     * 新增物品信息
     * 
     * @param busMentGoods 物品信息
     * @return 结果
     */
    @Override
    public int insertBusMentGoods(BusMentGoods busMentGoods)
    {
        // 校验参数
        Assert.isNull(busMentGoods.getId(),"你是?");
        String goodsName = busMentGoods.getGoodsName();
        String brand = busMentGoods.getBrand();
        String spec = busMentGoods.getSpec();
        String goodsDesc = busMentGoods.getGoodsDesc();
        Assert.hasLength(goodsName,"物品名称不能为0");
        Assert.state(goodsName.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"名称不超过50字");
        Assert.state(brand.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"品牌不超过50字");
        Assert.state(spec.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"规格不超过50字");
        Assert.state(goodsDesc.length() < BusinessManagementConstants.STRING_NUMBER_TWO_HUNDRED,"描述不超过200字");
        Assert.notNull(busMentGoods.getCategoryId(),"分类id不能为空");
        Assert.hasLength(busMentGoods.getGoodsCover(),"图片不能为空");

        return busMentGoodsMapper.insertBusMentGoods(busMentGoods);
    }

    /**
     * 修改物品信息
     * 
     * @param busMentGoods 物品信息
     * @return 结果
     */
    @Override
    public int updateBusMentGoods(BusMentGoods busMentGoods)
    {
        // 校验参数
        Assert.notNull(busMentGoods.getId(),"非法参数");
        String goodsName = busMentGoods.getGoodsName();
        String brand = busMentGoods.getBrand();
        String spec = busMentGoods.getSpec();
        String goodsDesc = busMentGoods.getGoodsDesc();
        Assert.hasLength(goodsName,"物品名称不能为0");
        Assert.state(goodsName.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"名称不超过50字");
        Assert.state(brand.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"品牌不超过50字");
        Assert.state(spec.length() < BusinessManagementConstants.STRING_NUMBER_FIFTY,"规格不超过50字");
        Assert.state(goodsDesc.length() < BusinessManagementConstants.STRING_NUMBER_TWO_HUNDRED,"描述不超过200字");
        Assert.notNull(busMentGoods.getCategoryId(),"分类id不能为空");
        Assert.hasLength(busMentGoods.getGoodsCover(),"图片不能为空");
        return busMentGoodsMapper.updateBusMentGoods(busMentGoods);
    }

    /**
     * 批量删除物品信息
     * 
     * @param ids 需要删除的物品信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentGoodsByIds(Long[] ids)
    {
        return busMentGoodsMapper.deleteBusMentGoodsByIds(ids);
    }

    /**
     * 删除物品信息信息
     * 
     * @param id 物品信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentGoodsById(Long id)
    {
        Assert.notNull(id,"非法参数");
        BusMentGoods busMentGoods = busMentGoodsMapper.selectBusMentGoodsById(id);
        Assert.notNull(busMentGoods,"非法参数");
        // todo  有关明细的校验


        return busMentGoodsMapper.deleteBusMentGoodsById(id);
    }
}
